﻿/// <reference path='../../typings/angularjs/angular.d.ts'/>
/// <reference path='../../typings/angularjs/angular-resource.d.ts'/>
/// <reference path="../app.ts" />

appRoot.controller('RootController',
    ['$scope', '$routeParams', '$location', '$rootScope', 'staticObjects', 'notificationFactory', 'logFactory', 'AuthenticationService',
        ($scope, $routeParams, $location, $rootScope, staticObjects: IStaticObjects, notificationFactory: INotificationFactory, logFactory: ILogFactory, AuthenticationService: Sustainalytics.Authentication.IAuthenticationService) => {

            //$scope.userInfo = $rootScope.userInfo;
            //$rootScope.$watch('userInfo', function () {
            //    $scope.userInfo = $rootScope.userInfo;
            //});

            $scope.$on('$routeChangeSuccess', (e, current, previous) => {
                $scope.activeViewPath = $location.path();

            });

            $scope.isActive = (path) => {
                if ($location.path().substr(0, path.length) == path) {
                    if (path == "/" && $location.path() == "/") {
                        return true;
                    }
                    else {
                        if (path == "/") {
                            return false;
                        }
                    }
                    return true;
                }
                else {
                    return false;
                }
            };

            $scope.logout = () => {
                AuthenticationService.logout();
            };

            var errorCallback = (data, status, headers, config) => {
                $scope.loading = false;
                if (data != "") {
                    notificationFactory.error(data.ExceptionMessage, data.message);
                }
                else {
                    notificationFactory.error(status, staticObjects.getErrorMesageByStatusCode(status));
                }
            };
        }]);
